<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 页面设计
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;

class DesignerPage extends Common
{
    public function initialize(){
        parent::initialize();
    }
	//列表
	public function index(){
		$page = input('param.page');
		$limit = input('param.limit');
		if(input('param.field') && input('param.order')){
			$order = input('param.field').' '.input('param.order');
		}else{
			$order = Db::raw('if(ishome=0,999,ishome),id');
		}
		$where = array();
		$where[] = ['aid','=',aid];
		$where[] = ['bid','=',bid];
		if(input('param.name')) $where[] = ['name','like','%'.input('param.name').'%'];
		if(input('?param.ishome')) $where[] = ['ishome','=',input('param.ishome')];
        if(request()->isAjax()){
			$count = 0 + Db::name('designerpage')->where($where)->count();
			$data = Db::name('designerpage')->where($where)->page($page,$limit)->order($order)->select()->toArray();
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}
		$count = 0 + Db::name('designerpage')->where($where)->count();
		$datalist = Db::name('designerpage')->where($where)->limit(10)->order($order)->select()->toArray();
		View::assign('count',$count);
		View::assign('datalist',$datalist);
        if($this->auth_data == 'all' || in_array('DesignerPage/*',$this->auth_data) || in_array('DesignerPage/sethome',$this->auth_data)){
            $auth['setHome'] = true;
        }else{
            $auth['setHome'] = false;
        }
        if($this->auth_data == 'all' || in_array('DesignerPage/*',$this->auth_data) || in_array('DesignerPage/rehome',$this->auth_data)){
            $auth['rehome'] = true;
        }else{
            $auth['rehome'] = false;
        }
        if($this->auth_data == 'all' || in_array('DesignerPage/*',$this->auth_data) || in_array('DesignerPage/del',$this->auth_data)){
            $auth['del'] = true;
        }else{
            $auth['del'] = false;
        }
        View::assign('auth',$auth);
		return View::fetch();
	}
	//编辑
	public function edit(){
		if(request()->isAjax()){
			$id = input('post.id/d');
			$content = input('post.content');
			$pageinfo = input('post.pageinfo');
			if($id) $designerpage = Db::name('designerpage')->where('aid',aid)->where('bid',bid)->where('id',$id)->find();
			$info = array();
			$info['content'] = \app\common\Common::geteditorcontent($content);
			$info['pageinfo'] = $pageinfo;
			$pageinfo = json_decode($pageinfo,true);
			$info['name'] = $pageinfo[0]['params']['title'];

            if(!$designerpage){
				$info['createtime'] = time();
				$info['aid'] = aid;
				$info['bid'] = bid;
			}
			$info['updatetime'] = time();

			if($designerpage){
				Db::name('designerpage')->where('aid',aid)->where('bid',bid)->where('id',$designerpage['id'])->update($info);
				$pageid = $designerpage['id'];
				\app\common\System::plog('编辑自定义页面'.$pageid);
			}else{
				$pageid = Db::name('designerpage')->insertGetId($info);
				\app\common\System::plog('添加自定义页面');
			}
			return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('index'),'pageid'=>$pageid]);
		}
		if(input('param.id')){
			$info = Db::name('designerpage')->where('aid',aid)->where('bid',bid)->where('id',input('param.id/d'))->find();
		}else{
			$info = array('id'=>'');
		}
		$pagedata= $info['content'] && json_decode($info['content']) ? $info['content'] : '[]';
		$pagedata = \app\common\System::initpagecontent($pagedata,aid);
		$pageinfo= $info['pageinfo'] && json_decode($info['pageinfo']) ? $info['pageinfo'] : "[{id:'M0000000000000',temp:'topbar',params:{title:'默认标题',gid:0,'bgcolor':'#f5f5f5',quanxian:{'all':true},fufei:'0',showgg:0,guanggao:'".PRE_URL.'/static/imgsrc/picture-1.jpg'."',hrefurl:'',ggrenqun:{'0':true},cishu:'0'}}]";
		$dc = getcustom('restaurant') ? 1 : 0;
        View::assign('dc',$dc);
        if(in_array($this->user['isadmin'],[1,2])){
            View::assign('isadmin',true);
        }

		View::assign('info',$info);
		View::assign('pagedata',$pagedata);
		View::assign('pageinfo',$pageinfo);
		return View::fetch();
	}
	//编辑选项卡内容
	public function designer_tab(){
		$pagetitle = input('param.tabname');
		$tabid = input('param.tabid');
		$tabindexid = input('param.tabindexid');
		$info = Db::name('designerpage_tab')->where('aid',aid)->where('tabid',$tabid)->where('tabindexid',$tabindexid)->find();
		$pagedata= $info['content'] && json_decode($info['content']) ? $info['content'] : '[]';
		View::assign('pagetitle',$pagetitle);
		View::assign('pagedata',$pagedata);

		return View::fetch();
	}
	public function designer_tab_save(){
		$tabid = input('post.tabid');
		$tabindexid = input('post.tabindexid');
		$content = input('post.tabpagedata');

		$designerpage = Db::name('designerpage_tab')->where('aid',aid)->where('tabid',$tabid)->where('tabindexid',$tabindexid)->find();
		$info = array();
		$info['content'] = \app\common\Common::geteditorcontent($content);
		if(!$designerpage){
			$info['createtime'] = time();
			$info['aid'] = aid;
			$info['tabid'] = $tabid;
			$info['tabindexid'] = $tabindexid;
		}
		$info['updatetime'] = time();
		if($designerpage){
			Db::name('designerpage_tab')->where('aid',aid)->where('tabid',$tabid)->where('tabindexid',$tabindexid)->update($info);
			$pageid = $designerpage['id'];
		}else{
			$pageid = Db::name('designerpage_tab')->insertGetId($info);
		}
		return json(['status'=>1,'msg'=>'操作成功','pageid'=>$pageid]);
	}

	//设置成主页
	public function sethome(){
		$id = input('post.id/d');
		$ishome = input('post.ishome/d');
        if(empty($id)) {
            return json(['status'=>0,'msg'=>'参数错误']);
        }
		Db::name('designerpage')->where('aid',aid)->where('bid',bid)->where('ishome',$ishome)->update(['ishome'=>0]);
		Db::name('designerpage')->where('aid',aid)->where('bid',bid)->where('id',$id)->update(['ishome'=>$ishome]);
		\app\common\System::plog('设置设计页面为主页'.$id);
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//取消首页
	public function rehome(){
		$id = input('post.id/d');
        if(empty($id)) {
            return json(['status'=>0,'msg'=>'参数错误']);
        }
		Db::name('designerpage')->where('aid',aid)->where('bid',bid)->where('id',$id)->update(['ishome'=>0]);
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//复制
	public function copy(){
		$id = input('post.id/d');
        if(empty($id)) {
            return json(['status'=>0,'msg'=>'参数错误']);
        }
		$copydata = Db::name('designerpage')->where('aid',aid)->where('bid',bid)->where('id',$id)->find();
		$copydata['id'] = '';
		$copydata['ishome'] = 0;
		$copydata['createtime'] = time();
		$copydata['updatetime'] = time();
		$id = Db::name('designerpage')->insertGetId($copydata);
		\app\common\System::plog('复制设计页面'.$id);
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//删除
	public function del(){
		$id = input('post.id/d');
        if(empty($id)) {
            return json(['status'=>0,'msg'=>'参数错误']);
        }
		Db::name('designerpage')->where('aid',aid)->where('bid',bid)->where('id',$id)->delete();
		\app\common\System::plog('删除设计页面'.$id);
		return json(['status'=>1,'msg'=>'删除成功']);
	}
	//文本自定义
	public function textset(){
		if(bid > 0) showmsg('无权限访问');
		if(request()->isPost()){
			$info = input('post.info/a');
			\think\facade\Cache::set('textset_'.aid,$info);
			\app\common\System::plog('设置文本自定义');
			return json(['status'=>1,'msg'=>'设置成功','url'=>true]);
		}

		$textset = \think\facade\Cache::get('textset_'.aid);
		if(!$textset) $textset = ['余额'=>'余额','积分'=>'积分','佣金'=>'佣金','优惠券'=>'优惠券','会员'=>'会员'];
		View::assign('info',$textset);
		return View::fetch();
	}
	//预览
	public function preview(){
		$id = input('param.id/d');
		if($id){
			$info = Db::name('designerpage')->where('aid',aid)->where('id',$id)->find();
		}
		$pagedata= $info['content'] && json_decode($info['content']) ? $info['content'] : '[]';
		$pagedata = \app\common\System::initpagecontent($pagedata,aid);
		$pageinfo= $info['pageinfo'] && json_decode($info['pageinfo']) ? $info['pageinfo'] : "[{id:'M0000000000000',temp:'topbar',params:{title:'',desc:'',img:'',kw:'',footer:'1',footermenu:'', floatico:'0',floatstyle:'right',floatwidth:'30px',floattop:'100px',floatimg:'',floatlink:''}}]";
		$sysset = Db::name('admin_set')->where('aid',aid)->find();
		if($info['bid']!=0){
			$business = Db::name('business')->where('aid',aid)->where('id',$info['bid'])->find();
			$sysset['name'] = $business['name'];
			$sysset['logo'] = $business['logo'];
		}

        $dc = getcustom('restaurant') ? 1 : 0;
        View::assign('dc',$dc);
		View::assign('info',$info);
		View::assign('pagedata',$pagedata);
		View::assign('pageinfo',$pageinfo);
		View::assign('sysset',$sysset);
		return View::fetch();
	}

	//选择坐标
	public function choosezuobiao(){
		return View::fetch();
	}
	//选择链接
	public function chooseurl(){
		//商品分类
		$clist = Db::name('shop_category')->Field('id,name')->where('aid',aid)->where('pid',0)->order('sort desc,id')->select()->toArray();
		foreach($clist as $k=>$v){
			$child = Db::name('shop_category')->Field('id,name')->where('aid',aid)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray();
			foreach($child as $k2=>$v2){
				$child2 = Db::name('shop_category')->Field('id,name')->where('aid',aid)->where('pid',$v2['id'])->order('sort desc,id')->select()->toArray();
				$child[$k2]['child'] = $child2;
			}
			$clist[$k]['child'] = $child;
		}
		if(bid > 0){
			$clist2 = Db::name('shop_category2')->Field('id,name')->where('aid',aid)->where('bid',bid)->where('pid',0)->order('sort desc,id')->select()->toArray();
			foreach($clist2 as $k=>$v){
				$child = Db::name('shop_category2')->Field('id,name')->where('aid',aid)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray();
				foreach($child as $k2=>$v2){
					$child2 = Db::name('shop_category2')->Field('id,name')->where('aid',aid)->where('pid',$v2['id'])->order('sort desc,id')->select()->toArray();
					$child[$k2]['child'] = $child2;
				}
				$clist2[$k]['child'] = $child;
			}
		}else{
			$clist2 = [];
		}
		//商品分组
		$glist = Db::name('shop_group')->where('aid',aid)->order('sort desc,id')->select()->toArray();
		//文章分类
		$aclist = Db::name('article_category')->Field('id,name,pic')->where('aid',aid)->where('bid',bid)->where('status',1)->where('pid',0)->order('sort desc,id')->select()->toArray();
		foreach($aclist as $k=>$v){
			$aclist[$k]['child'] = Db::name('article_category')->Field('id,name,pic')->where('aid',aid)->where('status',1)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray();
		}
		$bclist = Db::name('business_category')->where('aid',aid)->order('sort desc,id')->select()->toArray();
		View::assign('bclist',$bclist);
		View::assign('clist',$clist);
		View::assign('clist2',$clist2);
		View::assign('glist',$glist);
		View::assign('aclist',$aclist);
        if(bid != 0){
			return View::fetch('chooseurl2');
		}
		return View::fetch();
	}
	//选择链接 用于设置多商户默认底部菜单
	public function chooseurl3(){
		//商品分类
		$clist = Db::name('shop_category')->Field('id,name')->where('aid',aid)->where('pid',0)->order('sort desc,id')->select()->toArray();
		foreach($clist as $k=>$v){
			$child = Db::name('shop_category')->Field('id,name')->where('aid',aid)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray();
			foreach($child as $k2=>$v2){
				$child2 = Db::name('shop_category')->Field('id,name')->where('aid',aid)->where('pid',$v2['id'])->order('sort desc,id')->select()->toArray();
				$child[$k2]['child'] = $child2;
			}
			$clist[$k]['child'] = $child;
		}
		//商品分组
		$glist = Db::name('shop_group')->where('aid',aid)->order('sort desc,id')->select()->toArray();
		//文章分类
		$aclist = Db::name('article_category')->Field('id,name,pic')->where('aid',aid)->where('bid',bid)->where('status',1)->where('pid',0)->order('sort desc,id')->select()->toArray();
		foreach($aclist as $k=>$v){
			$aclist[$k]['child'] = Db::name('article_category')->Field('id,name,pic')->where('aid',aid)->where('status',1)->where('pid',$v['id'])->order('sort desc,id')->select()->toArray();
		}

		$bclist = Db::name('business_category')->where('aid',aid)->order('sort desc,id')->select()->toArray();
		View::assign('bclist',$bclist);
		View::assign('clist',$clist);
		View::assign('glist',$glist);
		View::assign('aclist',$aclist);
		return View::fetch();
	}

	public function api(){
		if(input('param.op') == 'selectproduct'){//查询商品
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];
			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
                $chidlc = Db::name('shop_category')->where('aid',aid)->where('pid',$cid)->column('id');
                if($chidlc){
                    $chidlc = array_merge($chidlc, [$cid]);
                    $whereCid = '(';
                    foreach($chidlc as $k => $c){
                        if(count($chidlc) == ($k + 1))
                            $whereCid .= "find_in_set({$c},cid)";
                        else
                            $whereCid .= " find_in_set({$c},cid) or ";
                    }
                    $where[] = Db::raw($whereCid . ')');
                }else{
                    $where[] = Db::raw("find_in_set({$cid},cid)");
                }
			}
			if($_POST['cid2']){
				$cid2 = intval($_POST['cid2']);
				if($cid2 > 0){
					$chidlc2 = Db::name('shop_category2')->where('aid',aid)->where('pid',$cid2)->column('id');
					if($chidlc2){
						$chidlc2 = array_merge($chidlc2, [$cid2]);
						$whereCid2 = '(';
						foreach($chidlc2 as $k => $c){
							if(count($chidlc2) == ($k + 1))
								$whereCid2 .= "find_in_set({$c},cid2)";
							else
								$whereCid2 .= " find_in_set({$c},cid2) or ";
						}
						$where[] = Db::raw($whereCid2 . ')');
					}else{
						$where[] = Db::raw("find_in_set({$cid2},cid2)");
					}
				}
			}
			if($_POST['giddata']){
				$_string = array();
				foreach($_POST['giddata'] as $gid=>$istrue){
					$gid = strval($gid);
					if($istrue=='true'){
						if($gid == 'all'){
							$_string[] = "1=1";
						}elseif($gid == '0'){
							$_string[] = "gid is null or gid=''";
						}else{
							$_string[] = "find_in_set({$gid},gid)";
						}
					}
				}
				if(!$_string){
					$where2 = '0=1';
				}else{
					$where2 = implode(" or ",$_string);
				}
			}else{
				$where2 = '1=1';
			}
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			if($_POST['sortby'] == 'rand') $order = Db::raw('rand()');
            $field = 'id,bid,name,pic,sell_price,market_price,sales,price_type';
            $result = Db::name('shop_product')->field($field)->where($where)->where($where2)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			if(!$result) $result = [];
            return $result;
		}
        if(input('param.op') == 'selectRestaurantproduct'){//查询菜品
            $where = [];
            $where[] = ['aid','=',aid];
            $where[] = ['status','=',1];
            $where[] = ['ischecked','=',1];
            if($_POST['kw']){
                $where[] = ['name','like','%'.$_POST['kw'].'%'];
            }
            if($_POST['cid']){
                $cid = intval($_POST['cid']);
                $where[] = Db::raw("find_in_set({$cid},cid)");
            }
            if($_POST['giddata']){
                $_string = array();
                foreach($_POST['giddata'] as $gid=>$istrue){
                    if($istrue=='true'){
                        if($gid == 'all'){
                            $_string[] = "1=1";
                        }elseif($gid == '0'){
                            $_string[] = "gid is null or gid=''";
                        }else{
                            $_string[] = "find_in_set({$gid},gid)";
                        }
                    }
                }
                if(!$_string){
                    $where2 = '0=1';
                }else{
                    $where2 = implode(" or ",$_string);
                }
            }else{
                $where2 = '1=1';
            }
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}

            $order = 'sort desc';
            if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
            if($_POST['sortby'] == 'createtimedesc') $order = 'create_time desc';
            if($_POST['sortby'] == 'createtime') $order = 'create_time';
            if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
            if($_POST['sortby'] == 'rand') $order = Db::raw('rand()');
            $result = Db::name('restaurant_product')->field('id,name,pic,sell_price,market_price,sales')->where($where)->where($where2)->order($order)->limit($_POST['proshownum'])->select()->toArray();
            if(!$result) $result = [];
            //echo M()->_sql();
            return $result;
        }
		if(input('param.op') == 'selectcollage'){//查询拼团商品
			$where = [];
			$where[] = ['aid','=',aid];
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}
			$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];

			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
				$chidlc = Db::name('collage_category')->where('aid',aid)->where('pid',$cid)->select()->toArray();
				if($chidlc){
					$cids = [$cid];
					foreach($chidlc as $c){
						$cids[] = intval($c['id']);
					}
					$where[] = ['cid','in',$cids];
				}else{
					$where[] = ['cid','=',$cid];
				}
			}
			if($_POST['giddata']){
				$_string = array();
				foreach($_POST['giddata'] as $gid=>$istrue){
					if($istrue=='true'){
						if($gid == '0'){
							$_string[] = "gid is null or gid=''";
						}else{
							$_string[] = "find_in_set({$gid},gid)";
						}
					}
				}
				if(!$_string){
					$where2 = '0=1';
				}else{
					$where2 = implode(" or ",$_string);
				}
			}else{
				$where2 = '1=1';
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			$result = Db::name('collage_product')->field('id,name,pic,sell_price,market_price,sales,teamnum')->where($where)->where($where2)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			if(!$result) $result = [];
			//echo Db::name()->getlastsql();
			return $result;
		}
        if(input('param.op') == 'selectcycle'){//查询周期购商品
            $where = [];
            $where[] = ['aid','=',aid];
            if(input('param.bid')!==''){
                $where[] = ['bid','=',input('param.bid/d')];
            }
            $where[] = ['status','=',1];
            $where[] = ['ischecked','=',1];

            if($_POST['kw']){
                $where[] = ['name','like','%'.$_POST['kw'].'%'];
            }
            if($_POST['cid']){
                $cid = intval($_POST['cid']);
                $chidlc = Db::name('cycle_category')->where('aid',aid)->where('pid',$cid)->select()->toArray();
                if($chidlc){
                    $cids = [$cid];
                    foreach($chidlc as $c){
                        $cids[] = intval($c['id']);
                    }
                    $where[] = ['cid','in',$cids];
                }else{
                    $where[] = ['cid','=',$cid];
                }
            }
            if($_POST['giddata']){
                $_string = array();
                foreach($_POST['giddata'] as $gid=>$istrue){
                    if($istrue=='true'){
                        if($gid == '0'){
                            $_string[] = "gid is null or gid=''";
                        }else{
                            $_string[] = "find_in_set({$gid},gid)";
                        }
                    }
                }
                if(!$_string){
                    $where2 = '0=1';
                }else{
                    $where2 = implode(" or ",$_string);
                }
            }else{
                $where2 = '1=1';
            }

            $order = 'sort desc';
            if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
            if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
            if($_POST['sortby'] == 'createtime') $order = 'createtime';
            if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
            $result = Db::name('cycle_product')->field('id,name,pic,sell_price,market_price,sales,ps_cycle')->where($where)->where($where2)->order($order)->limit($_POST['proshownum'])->select()->toArray();
          if($result){
              foreach($result as $key=>&$val){
                  $ps_cycle = ['1' => '每日一期','2' => '每周一期' ,'3' => '每月一期'];
                  $val['pspl'] = $ps_cycle[$val['ps_cycle']];
              }
          }
            if(!$result) $result = [];
            //echo Db::name()->getlastsql();
            return $result;
        }
		if(input('param.op') == 'selectluckycollage'){//查询幸运拼团商品
			$where = [];
			$where[] = ['aid','=',aid];
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}
			$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];

			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
				$chidlc = Db::name('lucky_collage_category')->where('aid',aid)->where('pid',$cid)->select()->toArray();
				if($chidlc){
					$cids = [$cid];
					foreach($chidlc as $c){
						$cids[] = intval($c['id']);
					}
					$where[] = ['cid','in',$cids];
				}else{
					$where[] = ['cid','=',$cid];
				}
			}
			if($_POST['giddata']){
				$_string = array();
				foreach($_POST['giddata'] as $gid=>$istrue){
					if($istrue=='true'){
						if($gid == '0'){
							$_string[] = "gid is null or gid=''";
						}else{
							$_string[] = "find_in_set({$gid},gid)";
						}
					}
				}
				if(!$_string){
					$where2 = '0=1';
				}else{
					$where2 = implode(" or ",$_string);
				}
			}else{
				$where2 = '1=1';
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			$result = Db::name('lucky_collage_product')->field('id,name,pic,sell_price,market_price,sales,teamnum,gua_num,fy_money,fy_money_val,fy_type')->where($where)->where($where2)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			foreach($result as $k=>$r){
				$result[$k]['money'] = $r['fy_money_val'];
				if($r['fy_type']==1){
					$result[$k]['money'] = $r['sell_price']*$r['fy_money'];
				}

			}
			if(!$result) $result = [];

			return $result;
		}
		if(input('param.op') == 'selectkanjia'){//查询砍价商品
			$where = [];
			$where[] = ['aid','=',aid];
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}
			$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];

			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			$result = Db::name('kanjia_product')->field('id,name,pic,sell_price,min_price,sales')->where($where)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			if(!$result) $result = [];
			return $result;
		}
		if(input('param.op') == 'selectseckill'){//查询秒杀商品
			$where = [];
			$where[] = ['aid','=',aid];
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}
			$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];

			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			$result = Db::name('seckill_product')->field('id,name,pic,sell_price,market_price,sales')->where($where)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			if(!$result) $result = [];
			return $result;
		}

		if(input('param.op') == 'selectscoreshop'){//查询积分兑换商品
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['status','=',1];
			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
				$chidlc = Db::name('scoreshop_category')->where('aid',aid)->where('pid',$cid)->select()->toArray();
				if($chidlc){
					$cids = [$cid];
					foreach($chidlc as $c){
						$cids[] = intval($c['id']);
					}
					$where[] = ['cid','in',$cids];
				}else{
					$where[] = ['cid','=',$cid];
				}
			}
			if($_POST['giddata']){
				$_string = array();
				foreach($_POST['giddata'] as $gid=>$istrue){
					if($istrue=='true'){
						if($gid == '0'){
							$_string[] = "gid is null or gid=''";
						}else{
							$_string[] = "find_in_set({$gid},gid)";
						}
					}
				}
				if(!$_string){
					$where2 = '0=1';
				}else{
					$where2 = implode(" or ",$_string);
				}
			}else{
				$where2 = '1=1';
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			$result = Db::name('scoreshop_product')->field('id,name,pic,sell_price,score_price,money_price,sales')->where($where)->where($where2)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			if(!$result) $result = [];
			//echo M()->_sql();
			return $result;
		}

		if(input('param.op') == 'selecttuangou'){//查询团购商品
			$where = [];
			$where[] = ['aid','=',aid];
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}
			$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];

			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
				$chidlc = Db::name('tuangou_category')->where('aid',aid)->where('pid',$cid)->select()->toArray();
				if($chidlc){
					$cids = [$cid];
					foreach($chidlc as $c){
						$cids[] = intval($c['id']);
					}
					$where[] = ['cid','in',$cids];
				}else{
					$where[] = ['cid','=',$cid];
				}
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			$result = Db::name('tuangou_product')->field('id,name,pic,sell_price,market_price,sales,pricedata')->where($where)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			if(!$result) $result = [];
			foreach($result as $k2=>$v2){
				$buynum = $v2['sales'];
				$pricedata = json_decode($v2['pricedata'],true);
				$nowpricedata = array('num'=>0,'money'=>$v2['sell_price']);
				foreach($pricedata as $k3=>$v3){
					if($buynum >= $v3['num']){
						$nowpricedata = $v3;
					}
				}
				$v2['sell_price'] = $nowpricedata['money'];
				$minpricedata = end($pricedata);
				$min_price = $minpricedata['money'];
				$v2['min_price'] = $min_price;
				$result[$k2] = $v2;
			}
			//echo Db::name()->getlastsql();
			return $result;
		}
		if(input('param.op') == 'selectkecheng'){//查询课程
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['status','=',1];
			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
                $chidlc = Db::name('kecheng_category')->where('aid',aid)->where('pid',$cid)->column('id');
                if($chidlc){
                    $chidlc = array_merge($chidlc, [$cid]);
                    $whereCid = '(';
                    foreach($chidlc as $k => $c){
                        if(count($chidlc) == ($k + 1))
                            $whereCid .= "find_in_set({$c},cid)";
                        else
                            $whereCid .= " find_in_set({$c},cid) or ";
                    }
                    $where[] = Db::raw($whereCid . ')');
                }else{
                    $where[] = Db::raw("find_in_set({$cid},cid)");
                }
			}
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'join_num desc,sort desc';
			if($_POST['sortby'] == 'rand') $order = Db::raw('rand()');
			$result = Db::name('kecheng_list')->field('id,name,pic,price,market_price,join_num')->where($where)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			if(!$result) $result = [];
			foreach($result as $k2=>$v2){
				$result[$k2]['count'] = Db::name('kecheng_chapter')->where('kcid',$v2['id'])->where('status',1)->count();
			}
			return $result;
		}
		if(input('param.op') == 'selectarticle'){//查询文章
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['bid','=',input('param.bid/d')];
			$where[] = ['status','=',1];

			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
				$chidlc = Db::name('article_category')->where('aid',aid)->where('pid',$cid)->select()->toArray();
				if($chidlc){
					$cids = array($cid);
					foreach($chidlc as $c){
						$cids[] = intval($c['id']);
					}
					$where[] = ['cid','in',$cids];
				}else{
					$where[] = ['cid','=',$cid];
				}
			}
			if($_POST['giddata']){
				$_string = array();
				foreach($_POST['giddata'] as $gid=>$istrue){
					if($istrue=='true'){
						if($gid == '0'){
							$_string[] = "gid is null or gid=''";
						}else{
							$_string[] = "find_in_set({$gid},gid)";
						}
					}
				}
				if(!$_string){
					$where2 = '1=0';
				}else{
					$where2 = implode(" or ",$_string);
				}
			}else{
				$where2 = '1=1';
			}
			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'sendtimedesc') $order = 'sendtime desc';
			if($_POST['sortby'] == 'sendtime') $order = 'sendtime';
			if($_POST['sortby'] == 'readcount') $order = 'readcount desc,sort desc';
            $field = 'id,cid,name,subname,pic,sendtime,createtime,readcount';
            $result = Db::name('article')->field($field)->where($where)->where($where2)->order($order)->limit($_POST['shownum'])->select()->toArray();
			if(!$result) $result = array();
			//echo M()->_sql();
			$clist = Db::name('article_category')->where('aid',aid)->where('bid',bid)->select()->toArray();
			$cdata = array();
			foreach($clist as $c){
				$cdata[$c['id']] = $c['name'];
			}
			foreach($result as $k=>$v){
				$result[$k]['cname'] = $cdata[$v['cid']];
                }
			return $result;
		}
		if(input('param.op') == 'selectcoupon'){//查询优惠券
			$time = time();
			$where = "aid=".aid." and bid=".input('param.bid/d')." and unix_timestamp(starttime)<={$time} and unix_timestamp(endtime)>={$time}";
			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'stock') $order = 'stock desc,sort desc';
			$result = Db::name('coupon')->field('id,name,type,limit_count,money,createtime,minprice,stock')->where($where)->order($order)->limit($_POST['shownum'])->select()->toArray();
			if(!$result) $result = [];
			return $result;
		}

		if(input('param.op') == 'selectshortvideo'){//查询短视频
			$where = [];
			$where[] = ['aid','=',aid];
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}
			$where[] = ['status','=',1];
			if(input('param.cid')){
				$where[] = ['cid','=',input('param.cid/d')];
			}
			$order = 'sort desc';
			if(input('param.sortby') == 'sort') $order = 'sort desc,id desc';
			if(input('param.sortby') == 'sendtimedesc') $order = 'createtime desc';
			if(input('param.sortby') == 'sendtime') $order = 'createtime';
			if(input('param.sortby') == 'viewnum') $order = 'view_num desc,sort desc';
			$result = Db::name('shortvideo')->field('id,name,description,coverimg,createtime,view_num,zan_num')->where($where)->order($order)->limit(input('param.shownum'))->select()->toArray();
			if(!$result) $result = array();
			foreach($result as $k=>$v){
				if($v['bid']!=0){
					$result[$k]['logo'] = Db::name('business')->where('aid',aid)->where('id',$v['bid'])->value('logo');
				} else {
					$result[$k]['logo'] = Db::name('admin_set')->where('aid',aid)->value('logo');
				}
			}
			return $result;
		}
		if(input('param.op') == 'selectliveroom'){//查询直播间
			$time = time();
			$where = "aid=".aid." and status=1";
			$order = 'roomId desc';
			if($_POST['sortby'] == 'sort') $order = 'roomId desc';
			if($_POST['sortby'] == 'starttimedesc') $order = 'startTime desc';
			if($_POST['sortby'] == 'starttime') $order = 'startTime';
			if($_POST['sortby'] == 'endtimedesc') $order = 'endTime desc';
			if($_POST['sortby'] == 'endtime') $order = 'endTime';
			if($_POST['sortby'] == 'rand') $order = Db::raw('rand()');
			$result = Db::name('live_room')->field("id,bid,roomId,name,coverImg,shareImg,startTime,endTime,anchorName")->where($where)->order($order)->limit($_POST['shownum'])->select()->toArray();
			if(!$result) $result = [];
			$todaystart = strtotime(date('Y-m-d'));
			foreach($result as $k=>$v){
				$result[$k]['startTime'] = date('m-d H:i',$v['startTime']);
				$result[$k]['endTime'] = date('m-d H:i',$v['endTime']);
				$result[$k]['status'] = 1;
				if($v['startTime'] > time()){ //未开始
					$result[$k]['status'] = 0;
					if(date('Y-m-d') == date('Y-m-d',$v['startTime'])){
						$result[$k]['showtime'] = '今天'.date('H:i',$v['startTime']).'开播';
					}elseif(date('Y-m-d',time()+86400) == date('Y-m-d',$v['startTime'])){
						$result[$k]['showtime'] = '明天'.date('H:i',$v['startTime']).'开播';
					}elseif(date('Y-m-d',time()+86400*2) == date('Y-m-d',$v['startTime'])){
						$result[$k]['showtime'] = '后天'.date('H:i',$v['startTime']).'开播';
					}else{
						$result[$k]['showtime'] = date('m-d H:i',$v['startTime']).'开播';
					}
				}
				if($v['endTime'] < time()){ //已结束
					$result[$k]['status'] = 2;
				}
			}
			return $result;
		}
		if(input('param.op') == 'selectbusiness'){//查询商家
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['status','=',1];
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
				$where[] = ['cid','=',$cid];
			}
			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			if($_POST['sortby'] == 'scoredesc') $order = 'comment_score desc,sort desc';
			if($_POST['sortby'] == 'rand') $order = Db::raw('rand()');
			$result = Db::name('business')->field('id,name,logo,desc,createtime,sales,address,comment_score,content')->where($where)->order($order)->limit($_POST['shownum'])->select()->toArray();
			if(!$result) $result = [];

			$nowtime = time();
			$nowhm = date('H:i');
			foreach($result as $k=>$v){
				$statuswhere = "`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )";
				$prolist = Db::name('shop_product')->where('bid',$v['id'])->where('ischecked',1)->where($statuswhere)->field('id,pic,name,sales,market_price,sell_price')->limit(8)->order('sales desc,sort desc,id desc')->select()->toArray();
				$result[$k]['prolist'] = $prolist;
			}
			//echo M()->_sql();
			return $result;
		}

		/*查询预约服务商品*/
		if(input('param.op') == 'selectyuyue'){/*查询预约服务商品*/
			$where = [];
			$where[] = ['aid','=',aid];
			if(input('param.bid')!==''){
				$where[] = ['bid','=',input('param.bid/d')];
			}
			$where[] = ['status','=',1];
			$where[] = ['ischecked','=',1];
			if($_POST['cid']){
				$cid = intval($_POST['cid']);
				$where[] = ['cid','=',$cid];
			}
			if($_POST['kw']){
				$where[] = ['name','like','%'.$_POST['kw'].'%'];
			}

			$order = 'sort desc';
			if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
			if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
			if($_POST['sortby'] == 'createtime') $order = 'createtime';
			if($_POST['sortby'] == 'sales') $order = 'sales desc,sort desc';
			$result = Db::name('yuyue_product')->field('id,name,pic,sell_price,sales,danwei')->where($where)->order($order)->limit($_POST['proshownum'])->select()->toArray();
			if(!$result) $result = [];
			return $result;
		}



        if(input('param.op') == 'selectkecheng'){//查询课程
            $where = [];
            $where[] = ['aid','=',aid];
            $where[] = ['status','=',1];
            if($_POST['kw']){
                $where[] = ['name','like','%'.$_POST['kw'].'%'];
            }
            if($_POST['cid']){
                $cid = intval($_POST['cid']);
                $chidlc = Db::name('kecheng_category')->where('aid',aid)->where('pid',$cid)->column('id');
                if($chidlc){
                    $chidlc = array_merge($chidlc, [$cid]);
                    $whereCid = '(';
                    foreach($chidlc as $k => $c){
                        if(count($chidlc) == ($k + 1))
                            $whereCid .= "find_in_set({$c},cid)";
                        else
                            $whereCid .= " find_in_set({$c},cid) or ";
                    }
                    $where[] = Db::raw($whereCid . ')');
                }else{
                    $where[] = Db::raw("find_in_set({$cid},cid)");
                }
            }
            if(input('param.bid')!==''){
                $where[] = ['bid','=',input('param.bid/d')];
            }

            $order = 'sort desc';
            if($_POST['sortby'] == 'sort') $order = 'sort desc,id desc';
            if($_POST['sortby'] == 'createtimedesc') $order = 'createtime desc';
            if($_POST['sortby'] == 'createtime') $order = 'createtime';
            if($_POST['sortby'] == 'sales') $order = 'join_num desc,sort desc';
            if($_POST['sortby'] == 'rand') $order = Db::raw('rand()');
            $result = Db::name('kecheng_list')->field('id,name,pic,price,market_price,join_num')->where($where)->order($order)->limit($_POST['proshownum'])->select()->toArray();
            if(!$result) $result = [];
            foreach($result as $k2=>$v2){
                $result[$k2]['count'] = Db::name('kecheng_chapter')->where('kcid',$v2['id'])->where('status',1)->count();
            }
            return $result;
        }
        //zhaopin
        }

	//增加激励广告记录
	public function addRewardedVideoAd(){
		$data = [];
		$data['aid'] = aid;
		$data['unitid'] = input('post.unitid');
		$data['givescore'] = input('post.givescore/d');
		$data['givetimes'] = input('post.givetimes/d');
		$data['createtime'] = time();
		$id = Db::name('designerpage_rwvideoad')->insertGetId($data);
		return json(['status'=>1,'msg'=>'','adid'=>$id]);
	}
	//获取小程序二维码
	public function getwxqrcode(){
		$path = input('param.path');
		$rs = \app\common\Wechat::getQRCode(aid,'wx',$path);
		return json($rs);
	}
}